<?php
$inc_path = "../inc/";

require_once $inc_path . 'function.php';
require_once $inc_path . 'simple_html_dom.php';
//$old_error_reporting = error_reporting(0);


//$getOorU=new GetOpenriceOrUfood();
//$openrice_data=$getOorU->get_data('openrice', '103374');
//$ufood_data=$getOorU->get_data('ufood', '南丫島/建興亞婆豆腐花/10109000039');
//print_r($openrice_data);
//$data=$getOorU->get_data('ufood','','http://www.ufood.com.hk/restaurant/detail/%E4%B8%AD%E7%92%B0/Caprice/10108900052');
//print_r($data);
//print_r($ufood_data);
//echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
//$data=$getOorU->get_data_from_search_ufood('http://www.ufood.com.hk/restaurant/search/result.htm?distIds=1&foodTypeId=&mallIds=&mtrIds=&restaurantTypeId=&cuisineTypeId=&occationId=&avgSpendingId=&editorRecommend=&name=&nameType=');
//$data=$getOorU->get_data_from_search_openrice('http://www.openrice.com/restaurant/sr1.htm?s=1&district_id=1002&inputcategory=cname&inputstrrest=');
//print_r($data);

class GetOpenriceOrUfood {

	private $url;
	private $id;
	
	public function get_data($site,$id,$newurl=''){
		
        $url='';
        if($site=='openrice'&&!$newurl){
        	$url='http://www.openrice.com/restaurant/sr2.htm?shopid='.$id;
        }
        if($site=='ufood'&&!$newurl){
        	$str_url=explode('/', $id);
        	$url='http://www.ufood.com.hk/restaurant/detail/'.urlencode($str_url[0]).'/'.urlencode($str_url[1]).'/'.urlencode($str_url[2]);
        	$image_url='http://www.ufood.com.hk/restaurant/photo/'.urlencode($str_url[0]).'/'.urlencode($str_url[1]).'/'.urlencode($str_url[2]);
        }
        if($site=='openrice'&&$newurl){
        	$url=$newurl;
        }
        if($site=='ufood'&&$newurl){
        	$str_url=str_replace('http://','',$newurl);
        	$str_url_array=explode('/', $str_url);
        	foreach ($str_url_array as $i=>$value){
        		$str_url_array[$i]=urlencode($value);
        	}
        	$url=implode('/', $str_url_array);
        	$url='http://'.$url;
        	
        	$image_url=str_replace('detail','photo', $url);
        	
        }
        //$url=urlencode($url);
        //echo $url.'<br>';
        //echo $image_url;
         
        if($url){
        	$html_data =$this->get_url($url);
        	//echo $html_data;
        	$html = str_get_html($html_data);
        	unset($html_data);
        	$data=array();
        	if($site=='openrice'){
        		//Restaurant name
	        	foreach($html->find('html head title') as $element) {
					$head=array();
					$head=explode('-', $element->plaintext);
				}
				$data['name']=$head[0];
				
				//Restaurant logo image
        		foreach($html->find('div.restinfo div.restphoto img') as $element) {
					$logo_image=str_replace('//','', $element->src);
				}
				$data['logo_image']=$logo_image;

				//Restaurant info
        		$info='';
				foreach($html->find('div.restinfo table.addetail tr td div') as $k=>$element) {
					if($k==1){
						$info=$element->plaintext;
						break;
					}
				}
				$find_str=array('地址 :','英文地址：','電話 :','類別 :','消費 :');
				$replace_str=array('','++','++','++','++');
				$new_info=str_replace($find_str, $replace_str, $info);
				$new_info_array=explode('++', $new_info);
				$str=explode('vs', $new_info_array[4]);
				$new_info_array[4]=$str[0];
			    //print_r($new_info_array);
			    $data['address']=$new_info_array[0];
			    $data['end_address']=$new_info_array[1];
			    $data['phone']=$new_info_array[2];
			    $data['dishes']=$new_info_array[3];
			    $data['consumption']=$new_info_array[4];
			    
			    //Restaurant open time
			    $others_array=array();
				foreach($html->find('div.admoredetail table table table tr') as $i=>$element) {
					if($i!=0){
						$others_array[]=$element->last_child ()->plaintext;
					}
				}
				//print_r($others_array);
				$data['open_time']=$others_array[0];
				$data['pay_type']=$others_array[1];
				
		        //Restaurant images
		        $image_array=array();
				foreach($html->find('div.thumbnails img') as $j=>$element) {		
					$image_array[]=str_replace('//','',$element->src);
				}
				//print_r($image_array);
				$data['images']=$image_array;
        	}
        	
        	if($site=='ufood'){
        		//name
        		foreach($html->find('html head title') as $element) {
					$head=array();
					$head=explode('|', $element->plaintext);
				}
				//print_r($head);
				$data['name']=$head[0];
				
				//logo image
        		foreach($html->find('div.item_big_photo table tr img') as $element) {
					$logo_image='www.ufood.com.hk'.$element->src;
				}
				$data['logo_image']=$logo_image;
				
				//info
				$info=array();
        		foreach($html->find('div.rest_main_details_1 table tr') as $element) {
					$info[]=$element->last_child ()->plaintext;
				}
				$data['address']=$info[0];
				$data['phone']=$info[1];
				$data['dishes']=$info[2];
				$data['consumption']=$info[3];
				
				//unset($info);
        		//foreach($html->find('div.restaurant_details_box table tr') as $element) {
				//	$info[]=$element->last_child ()->plaintext;
				//}
        		unset($info);
        		foreach($html->find('div.restaurant_details_box table tr') as $element) {
					$info[]=$element->plaintext;
				}
				foreach ($info as $value){
				    if(strpos($value,'簡介')){
				    	$data['introduction']=str_replace('簡介', '', $value);
				    }else if(strpos($value,'網址')){
				    	$data['site_url']=str_replace('網址', '', $value);
				    }else if(strpos($value,'營業時間')){
				    	$data['opening_hours']=str_replace('營業時間', '', $value);
				    }else if(strpos($value,'付款方法')){
				    	$data['pay_type']=str_replace('付款方法', '', $value);
				    }else if(strpos($value,'訂桌資料')){
				    	$data['custom_tables']=str_replace('訂桌資料', '', $value);
				    }else if(strpos($value,'泊車資料')){
				    	$data['parking']=str_replace('泊車資料', '', $value);
				    }else if(strpos($value,'招牌菜')){
				    	$data['signature_dishes']=str_replace('招牌菜', '', $value);
				    }
				}
				//print_r($info);exit;
				
				//images
				unset($html);
				$html_data =$this->get_url($image_url);
        	    //echo $html_data;
        	    $html = str_get_html($html_data);
        	    unset($html_data);
        	    unset($image_array);
        		foreach($html->find('div#thumbs1 ul li div.img_holder img') as $element) {
					$image_array[]='www.ufood.com.hk'.$element->src;
				}
				$data['images']=$image_array;
        	    
        	}
        	unset($html);
        	return $data;        
        }
}
	
	public function get_url($url, $params = array()) {
		$CURL_OPTS = array(
			CURLOPT_CONNECTTIMEOUT => 10,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_TIMEOUT        => 60,
			CURLOPT_USERAGENT      => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
		);
		
		$ch = curl_init();
	
		$api_url = $url;
		if (count($params) > 0) {
			$api_url .= '?' . http_build_query($params, null, '&');
		}
	
		$opts = $CURL_OPTS;
		$opts[CURLOPT_URL] = $api_url;
	
		curl_setopt_array($ch, $opts);
		$result = curl_exec($ch);
	
		curl_close($ch);
		
		return $result;
	}

	public function getImage($url,$folder='',$filename='') {
		$saved_html_path = "D:/www/foodeasy/openrice/images";	
		if(!$url) return false;
		if(!$filename) {
			$ext=strrchr(strtolower($url),'.') ;
			if($ext!='.gif' && $ext!='.jpg' && $ext!='.png') return false ;
			$str=explode('/',$url) ;
			$filename=$str[count($str)-1] ;
		//print $filename.”\n”;
		}
		if($folder){
			$saved_html_path=$folder;
		}
		@mkdir($saved_html_path, 0777, true);
		$file=$saved_html_path.'/'.$filename;
		//echo $url;
		//echo $file; //exit;
		ob_start();
		readfile('http://'.$url);
		$img = ob_get_contents();
		ob_end_clean();
		$fp2=@fopen($file, 'a') ;
		fwrite($fp2,$img);
		fclose($fp2) ;
		return $file;
	}
	
	public function get_data_from_search_ufood($url){
		$html_data =$this->get_url($url);
        //echo $html_data;
        $html = str_get_html($html_data);
        unset($html_data);
        
        $data=array();
        $url_array=array();
        //item_info
		foreach($html->find('div.item_info div.name h2 a') as $element) {
			$url_array[]='http://www.ufood.com.hk'.$element->href;
		}
		//print_r($url_array);
		foreach ($url_array as $url){
		  $data[]=$this->get_data('ufood','',$url);
		}
		
		return $data;
        
	}
	
	public function get_data_from_search_openrice($url){
		
		$html_data =$this->get_url($url);
        //echo $html_data;
        $html = str_get_html($html_data);
        unset($html_data);
        
        $data=array();
        $url_array=array();
        //item_info
		foreach($html->find('div#restlist div.resttitle span a') as $element) {
			$url_array[]='http://www.openrice.com'.$element->href;
		}
		$new_url=array_unique($url_array);
		//print_r($new_url);exit;
		foreach ($new_url as $url){
		  $data[]=$this->get_data('openrice','',$url);
		}
		return $data;
	}


}